<?php

namespace App\Services;

//授权服务管理
use App\Models\Admin;
class AuthorityService
{
    //刷新权限和用户的关系绑定 user <==> permission
    //使用此方法 控制器等其他地方不需要关注 model_has_permissions 表
    public function refreshPermission()
    {
        $admins = Admin::query()->with(['roles'=>function($role){
            $role->with('permissions');
        }])->where('id','!=',1)->get();
        foreach ($admins as $admin){
            $permissionIds = [];
            foreach($admin->roles as $role){
                $permissionIds = array_merge($permissionIds , $role->permissions->pluck('id')->toArray());
            }
            $admin->syncPermissions($permissionIds); //授予用户权限 ,一次性撤消并添加新的权限
        }
    }
}
